import os, re
import const
from models import Module, PackageName, Package, FixedBug, ChangedPackage, Dependancy, OSRelease, DependancyOfBuild
from django.shortcuts import render, redirect, get_object_or_404, render_to_response
from django.utils import simplejson as json
from django.http import HttpResponse
from django.forms.models import model_to_dict
import const

def package_query(request):
    os_release_list = OSRelease.objects.all()
    return render(request, 'package_query.html', {'os_release_list':os_release_list})

def package_index(request):
    os_release_list = OSRelease.objects.all()
    return render(request, 'package_index.html', {'os_release_list':os_release_list})

def ajax_package_builds(request):
    if request.method != 'POST':
        return render(request, "error.html", {'dj_err': "Invalid Request"})
    os_release = request.POST.get(u'sel_os_release')
    build_list = list(DependancyOfBuild.objects.filter(os_release__name=os_release).values_list('build_no').distinct())
    return HttpResponse(json.dumps({'builds': build_list}))

def ajax_load_layermodules(request):
    if request.method != 'POST':
        return render(request, "error.html", {'dj_err': "Invalid Request"})
    os_release = request.POST.get(u'sel_os_release')
    build = request.POST.get(u'sel_build')
    package_list = Package.objects.filter(os_release__name=os_release,
        change_in_build=build).select_related('module__layer','module__name','package_name__name')
    layers = []
    for key, value in const.LAYERS.items():
        modules = list(Module.objects.filter(layer=value).values('id','name'))
        if modules:
            layer = {'layer_display':key, 'layer':value, 'modules':modules}
            layers.append(layer)
    for package in list(package_list):
        p_dict = model_to_dict(package)
        p_dict['change_flag_display'] = const.CHANGE_FLAGS_DISPLAY[package.get_change_flag_display()]
        p_dict['package_name__name'] = package.package_name.name
        p_layer = package.module.layer
        p_module_name = package.module.name
        for layer in layers:
            if layer['layer']==p_layer:
                modules = layer['modules']
                for module in modules:
                    if module['name']==p_module_name:
                        module.setdefault('pkg',[]).append(p_dict)
                        module.setdefault('pkg_exist',True)
                        break
                break
    return HttpResponse(json.dumps(layers))
    
def ajax_search_package(request):
    if request.method != 'POST':
        return render(request, "error.html", {'dj_err': "Invalid Request"})
    os_release = request.POST.get(u'sel_os_release')
    build = request.POST.get(u'sel_build')
    pkg_query = request.POST.get(u'pkg_query')
    depends = DependancyOfBuild.objects.filter(build_no=build, os_release__name=os_release, 
        relation__origin_package__name__icontains=pkg_query).values(
        'relation__origin_package__name', 'relation__origin_package__id').distinct()
    target_package_list = list(depends)
    return HttpResponse(json.dumps(target_package_list))

def ajax_search_hierarchy(request):
    if request.method != 'POST':
        return render(request, "error.html", {'dj_err': "Invalid Request"})
    os_release = request.POST.get(u'sel_os_release')
    target_pkg = request.POST.get(u'target_pkg')
    build = request.POST.get(u'sel_build')
###################to do include normal judge
    depends = list(DependancyOfBuild.objects.filter(build_no=build,
        os_release__name=os_release, relation__origin_package__id=target_pkg).values(
        'relation__depend_package__name', 'relation__depend_package__id'))
    affects = list(DependancyOfBuild.objects.filter(build_no=build,
        os_release__name=os_release, relation__depend_package__id=target_pkg).values(
        'relation__origin_package__name', 'relation__origin_package__id'))
    return HttpResponse(json.dumps({'depends':depends, 'affects':affects}))
